package com.yex.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yex.common.utils.PageUtils;
import com.yex.dao.BankDao;
import com.yex.entity.BankEntity;
import com.yex.service.BankService;
import com.yex.service.LocaleMessageSourceService;
import com.yex.service.OrderBankService;
import com.yex.utils.JsonResultVo;
import com.yex.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("bankService")
public class BankServiceImpl extends ServiceImpl<BankDao, BankEntity> implements BankService {
    @Autowired
    private LocaleMessageSourceService message;
    @Autowired
    private OrderBankService orderBankService;

    @Override
    public PageUtils queryPage(Map<String, Object> params, String filter) {
        Page<Map<String, Object>> page = new Query<Map<String, Object>>(params).getPage();
        List<Map<String, Object>> bankListEntityList = baseMapper.selectPageFilter(page, filter);
        page.setRecords(bankListEntityList);
        return new PageUtils(page);
    }

    @Override
    public JsonResultVo selectBankByUserId(Integer userId) {
        List<Map<String, Object>> mapList = baseMapper.selectBankByMap(userId);
        Map<String, Object> map = new HashMap<>(2);
        map.put("userBankList", mapList);
        return new JsonResultVo(message.getMessage("success"), map, 0);
    }


    @Override
    public List<BankEntity> selectBankListByUserId(Integer userId) {
        return baseMapper.selectBankByUserId(userId);
    }

    @Override
    @Transactional
    public JsonResultVo insertBank(BankEntity bankEntity) {
        Integer result = baseMapper.insert(bankEntity);
        return result > 0 ? new JsonResultVo(message.getMessage("success"), 0) : new JsonResultVo(message.getMessage("fail"), -1);
    }

    @Override
    @Transactional
    public JsonResultVo deleteBank(String id, Integer userId) {
        Integer result = baseMapper.updateBank(id, userId);
        return result > 0 ? new JsonResultVo(message.getMessage("success"), 0) : new JsonResultVo(message.getMessage("fail"), -1);
    }
}
